with open('input.txt') as file:
    record = {}
    numbers = list(map(int, file.read().strip().split(',')))
    for (i, n) in enumerate(numbers):
        record[n] = i
    
    last = numbers[-1]

    for t in range(len(numbers)-1, 2019):
        record[last], last = t, t - record.get(last, t)
    
    print(last)